package parser;
//TODO keep entry of folder so it can be used for main genre
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.compress.archivers.ArchiveException;
import org.xeustechnologies.jtar.TarEntry;
import org.xeustechnologies.jtar.TarInputStream;

import DBside.CDBox;
import DBside.JDBCConnection;
import DBside.JDBCConnection.Genres;
import DBside.TableItem;
import LogicSide.Uncompress;

import entities.CD;
import entities.Track;

public class UpdateByFile {

	private String file_path;
	private static List<CD> cds=new ArrayList<CD>();
	private static List<String> skipped=new ArrayList<String>();
	private static CDBox box=new CDBox();
	
	public static List<CD> excecute(String file_path) throws IOException, ArchiveException{	//executeUpdate(String archive_path) throws FileNotFoundException, IOException, ArchiveException{
		try{
		TarInputStream update_dir_root=Uncompress.compressed_to_file(file_path);
		JDBCConnection con=new JDBCConnection();
		TarEntry t=update_dir_root.getNextEntry();
		String dir_name="";
		ByteArrayInputStream b;
		while(t!=null)
		{
			if(t.isDirectory())
				dir_name=t.getName();
			else
			{
				processSingleFile(update_dir_root,dir_name,con,t);
				
			}
			t=update_dir_root.getNextEntry();
		}
		/*
		System.out.println("main dir: "+update_dir_root.getName()+"\n");
		if(update_dir_root.isDirectory())
		{
			for(File gen_dir:update_dir_root.listFiles())
			{				
				System.out.println("sub dir: "+gen_dir.getName()+"\n");
				if(!gen_dir.isDirectory())
				{
					System.out.println("error in subdir");
				}
				else
					for(File disc_file:gen_dir.listFiles())
					{
						if(!disc_file.isFile())
							System.out.println("error- dir instead of disc file");
						else
							processSingleFile(disc_file,gen_dir.getName(),con);
					}
			}
		}
		else
			System.out.println("error- extracted file is not an update files root directory");
		//con.executeBatch();
		 * 
		 */
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Compressed file not found");
		}
		System.out.println("Skipped: "+skipped);
		System.out.println("DONE");
		return cds;
	}
	private static void processSingleFile(InputStream disc_file, String main_gen_name,JDBCConnection con,TarEntry t) {
		CD cd = Parser.parseCD(disc_file);
		String s;
    	//if(System.getProperty("os.name").toLowerCase().contains("windows"))
    		//	s="\\\\";
    	//else
    		//s="/";

		if(cd==null)
			skipped.add(t.getName());
		else{
				boolean found_parent=false;
				if(found_parent){
					System.out.println("shit nigga");
				}
				for( Genres g : Genres.values())
				{
					//System.out.println(main_gen_name.toLowerCase().split("/")[0]);
					if(main_gen_name.toLowerCase().split("/")[0].equals(g.getName()))//split with / is for all os
					{
						found_parent = true;
						cd.setMainGenre(g);				
					}
				}
				if(!found_parent){
					cd.setMainGenre(Genres.NOTSUPPORTED);	
				}
				
				
					cds.add(cd);
				//box.addCD(cd);
		//System.out.println(cd);
		}
		}
}